package com.example.wisdomclassroomapi.facade.controller.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wisdomclassroomapi.application.service.CourseLessonPeriodApplicationService;
import com.example.wisdomclassroomapi.common.dto.PageDto;
import com.example.wisdomclassroomapi.common.util.IdentificationUtil;
import com.example.wisdomclassroomapi.domain.entity.CourseLessonPeriodEntity;
import com.example.wisdomclassroomapi.facade.assembler.CourseLessonPeriodFacadeAssembler;
import com.example.wisdomclassroomapi.facade.controller.common.BaseController;
import com.example.wisdomclassroomapi.facade.dto.CourseLessonPeriodRequestDto;
import com.example.wisdomclassroomapi.facade.dto.CourseLessonPeriodResponseDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import ws.schild.jave.EncoderException;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Package: com.example.wisdomclassroomapi.facade.controller.wxapp
 * @Description: <前端控制器>-相关接口
 * @Author: scholes
 * @CreateDate: 2022-04-18
 * @UpdateUser: scholes
 * @UpdateDate: 2022-04-18
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api/courseLessonPeriod")
@Slf4j
public class CourseLessonPeriodController extends BaseController {
    @Resource
    private CourseLessonPeriodApplicationService courseLessonPeriodApplicationService;

    @Resource
    private CourseLessonPeriodFacadeAssembler courseLessonPeriodFacadeAssembler;

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public PageDto<List<CourseLessonPeriodResponseDto>> getCourseLessonPeriodByPage(@RequestBody @Valid CourseLessonPeriodRequestDto request) {
        IPage<CourseLessonPeriodEntity> page = new Page<CourseLessonPeriodEntity>();
        page.setSize(request.getPageSize());
        page.setCurrent(request.getPageNum());
        CourseLessonPeriodEntity entity = courseLessonPeriodFacadeAssembler.assembleCourseLessonPeriodEntity(request);
        page = courseLessonPeriodApplicationService.page(entity, page);
        List<CourseLessonPeriodResponseDto> assembleData = new ArrayList<>();
        if (page != null && page.getRecords() != null && page.getRecords().size() > 0) {
            for (CourseLessonPeriodEntity item : page.getRecords()) {
                assembleData.add(courseLessonPeriodFacadeAssembler.assembleCourseLessonPeriodResponseDto(item));
            }
        }
        PageDto<List<CourseLessonPeriodResponseDto>> result = new PageDto<>();
        result.setPageSize((int) page.getSize());
        result.setPageNum((int) page.getCurrent());
        result.setData(assembleData);
        return result;
    }


    /**
     * 查询
     */
    @PostMapping("/list")
    public List<CourseLessonPeriodResponseDto> getCourseLessonPeriods(@RequestBody @Valid CourseLessonPeriodRequestDto request) {
        CourseLessonPeriodEntity entity = courseLessonPeriodFacadeAssembler.assembleCourseLessonPeriodEntity(request);
        List<CourseLessonPeriodEntity> list = courseLessonPeriodApplicationService.list(entity);
        List<CourseLessonPeriodResponseDto> assembleData = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (CourseLessonPeriodEntity item : list) {
                assembleData.add(courseLessonPeriodFacadeAssembler.assembleCourseLessonPeriodResponseDto(item));
            }
        }
        return assembleData;
    }

    /**
     * 获取某一实体
     */
    @PostMapping("/getCourseLessonPeriod")
    public CourseLessonPeriodResponseDto getCourseLessonPeriodDetails(String id) {
        CourseLessonPeriodEntity entity = courseLessonPeriodApplicationService.findById(id);
        CourseLessonPeriodResponseDto responseDto = null;
        if (entity != null) {
            responseDto = courseLessonPeriodFacadeAssembler.assembleCourseLessonPeriodResponseDto(entity);
        }
        return responseDto;
    }

    /**
     * 新增数据
     */
    @PostMapping("/add")
    public void saveCourseLessonPeriod(@RequestParam MultipartFile file, @RequestParam String lessonId, @RequestParam String title) throws IOException, EncoderException {
        CourseLessonPeriodEntity entity = new CourseLessonPeriodEntity();

        String duration = courseLessonPeriodApplicationService.getVideoTime(file).toString();

        String fileName = file.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        String filePath = "D:\\Develop\\docker\\ZLM\\";
        fileName = IdentificationUtil.shortUuid() + suffixName;

        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        file.transferTo(dest);

        String url = "http://localhost:8080/mp4/" + fileName;
        entity.setUrl(url);
        entity.setDuration(duration);
        entity.setLessonId(lessonId);
        entity.setTitle(title);

        courseLessonPeriodApplicationService.add(entity);
    }

    /**
     * 修改数据
     */
    @PostMapping("/update")
    public void modifyCourseLessonPeriod(@RequestBody CourseLessonPeriodEntity request) {
        courseLessonPeriodApplicationService.updateById(request);
    }

    /**
     * 删除数据
     */
    @PostMapping("/delete")
    public void removeCourseLessonPeriod(@RequestBody @Valid String id) {
        courseLessonPeriodApplicationService.deleteById(id);
    }

}